package com.zhangshuo.aspects;

import com.zhangshuo.common.dataSource.DBTypeEnum;
import com.zhangshuo.common.dataSource.DbContextHolder;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/5/31 0031.
 * 以service层进行切分
 */
@Component
@Aspect
public class DataSourceInterceptor {

    Logger logger = LoggerFactory.getLogger(DataSourceInterceptor.class);


    @Pointcut(value = "execution(public * com.zhangshuo.test1.mapper.**.*(..))")
    private void datasource1ServicePointcut() {
    }

    @Pointcut(value = "execution(public * com.zhangshuo.test2.mapper.**.*(..))")
    private void datasource2ServicePointcut() {
    }


    /**
     * 切换数据源1
     */
    @Before("datasource1ServicePointcut()")
    public void dataSource1Interceptor() {
        logger.info("切换到数据源{}..............................", "datasource1");
        DbContextHolder.setDbType(DBTypeEnum.datasource1);
    }

    /**
     * 切换数据源2
     */
    @Before("datasource2ServicePointcut()")
    public void dataSource2Interceptor() {
        logger.info("切换到数据源{}.......................", "datasource2");
        DbContextHolder.setDbType(DBTypeEnum.datasource2);
    }


}
